﻿//------------------------------------------------------------------------------
// System Name:   Customer Service Platform
// Component:     All Aera
// Created By:    Miles Chen (milesch@wicresoft.com)
// Created Date:  2011-2-20
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Business;
using System.ComponentModel;

namespace Wicresoft.Web.Controls
{
    /// <summary>
    /// 结合EntityPage的GridView, DetailsView使用，可自定义Action列
    /// </summary>
    public class ActionField : CommandField
    {
        public virtual bool ShowViewButton
        {
            get
            {
                object obj2 = base.ViewState["ShowViewButton"];
                return ((obj2 != null) && ((bool)obj2));
            }
            set
            {
                object obj2 = base.ViewState["ShowViewButton"];
                if ((obj2 == null) || (((bool)obj2) != value))
                {
                    base.ViewState["ShowViewButton"] = value;
                    this.OnFieldChanged();
                }
            }
        }
        public virtual bool ShowModifyButton
        {
            get
            {
                object obj2 = base.ViewState["ShowModifyButton"];
                return ((obj2 != null) && ((bool)obj2));
            }
            set
            {
                object obj2 = base.ViewState["ShowModifyButton"];
                if ((obj2 == null) || (((bool)obj2) != value))
                {
                    base.ViewState["ShowModifyButton"] = value;
                    this.OnFieldChanged();
                }
            }
        }
        public virtual bool ShowDisableButton
        {
            get
            {
                object obj2 = base.ViewState["ShowDisableButton"];
                return ((obj2 != null) && ((bool)obj2));
            }
            set
            {
                object obj2 = base.ViewState["ShowDisableButton"];
                if ((obj2 == null) || (((bool)obj2) != value))
                {
                    base.ViewState["ShowDisableButton"] = value;
                    this.OnFieldChanged();
                }
            }
        }
        public virtual bool ShowInsertMoreButton
        {
            get
            {
                object obj2 = base.ViewState["ShowInsertMoreButton"];
                return ((obj2 != null) && ((bool)obj2));
            }
            set
            {
                object obj2 = base.ViewState["ShowInsertMoreButton"];
                if ((obj2 == null) || (((bool)obj2) != value))
                {
                    base.ViewState["ShowInsertMoreButton"] = value;
                    this.OnFieldChanged();
                }
            }
        }
        public virtual bool ShowCloseButton
        {
            get
            {
                object obj2 = base.ViewState["ShowCloseButton"];
                return ((obj2 != null) && ((bool)obj2));
            }
            set
            {
                object obj2 = base.ViewState["ShowCloseButton"];
                if ((obj2 == null) || (((bool)obj2) != value))
                {
                    base.ViewState["ShowCloseButton"] = value;
                    this.OnFieldChanged();
                }
            }
        }

        public override bool ShowCancelButton
        {
            get
            {
                return base.Control is GridView ? false : base.ShowCancelButton;
            }
            set
            {
                base.ShowCancelButton = value;
            }
        }
        public override bool ShowDeleteButton
        {
            get
            {
                return base.Control is GridView ? false : base.ShowDeleteButton;
            }
            set
            {
                base.ShowDeleteButton = value;
            }
        }
        public override bool ShowEditButton
        {
            get
            {
                return base.Control is GridView ? false : base.ShowEditButton;
            }
            set
            {
                base.ShowEditButton = value;
            }
        }
        public override bool ShowInsertButton
        {
            get
            {
                return base.Control is GridView ? false : base.ShowInsertButton;
            }
            set
            {
                base.ShowInsertButton = value;
            }
        }
        public override bool ShowSelectButton
        {
            get
            {
                return base.Control is GridView ? false : base.ShowSelectButton;
            }
            set
            {
                base.ShowSelectButton = value;
            }
        }

        public override string CancelText
        {
            get
            {
                return base.CancelText;
            }
        }
        public override string DeleteText
        {
            get
            {
                return base.DeleteText;
            }
        }
        public override string EditText
        {
            get
            {
                return base.EditText;
            }
        }
        public override string InsertText
        {
            get
            {
                return base.InsertText;
            }
        }
        public override string SelectText
        {
            get
            {
                return base.SelectText;
            }
        }
        public override string UpdateText
        {
            get
            {
                return base.UpdateText;
            }
        }

        public override ButtonType ButtonType
        {
            get
            {
                return base.Control is GridView ? ButtonType.Link : ButtonType.Button;
            }
        }
        private bool ReadOnly
        {
            get
            {
                if (base.Control.Page is EntityPage)
                {
                    return (base.Control.Page as EntityPage).ReadOnly;
                }
                else
                    return false;
            }
        }

        protected override DataControlField CreateField()
        {
            return new ActionField();
        }

        protected override void CopyProperties(DataControlField newField)
        {
            ((ActionField)newField).ShowViewButton = this.ShowViewButton;
            ((ActionField)newField).ShowModifyButton = this.ShowModifyButton;
            ((ActionField)newField).ShowDisableButton = this.ShowDisableButton;

            ((ActionField)newField).ShowInsertMoreButton = this.ShowInsertMoreButton;
            ((ActionField)newField).ShowCloseButton = this.ShowCloseButton;
            base.CopyProperties(newField);
        }

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(cell, cellType, rowState, rowIndex);

            bool showViewButton = this.ShowViewButton;
            bool showModifyButton = this.ShowModifyButton;
            bool showDisableButton = this.ShowDisableButton;
            bool showInsertMoreButton = this.ShowInsertMoreButton;
            bool showCloseButton = this.ShowCloseButton;

            if (cellType == DataControlCellType.DataCell)
            {
                // GridView
                if (this.ButtonType == ButtonType.Link)
                {
                    if (showViewButton)
                    {
                        this.AddLinkToCell(cell, rowIndex, "View", "entity-view");
                        //cell.Controls.Add(new LiteralControl("&nbsp;"));
                    }
                    if (showModifyButton && !ReadOnly)
                    {
                        this.AddLinkToCell(cell, rowIndex, "Modify", "entity-modify");
                        //cell.Controls.Add(new LiteralControl("&nbsp;"));
                    }
                    if (showDisableButton && !ReadOnly)
                    {
                        this.AddLinkToCell(cell, rowIndex, "Disable", "entity-disable");
                        //cell.Controls.Add(new LiteralControl("&nbsp;"));
                    }
                }
                // DetailsView
                else
                {
                    DecorateButtons(cell);
                    cell.Controls.Add(new LiteralControl("&nbsp;"));

                    if (GetDetailsViewMode() != DetailsViewMode.ReadOnly && showInsertMoreButton && !ReadOnly)
                    {
                        this.AddButtonToCell(cell, rowIndex, "InsertMore", "entity-insert-more");
                        cell.Controls.Add(new LiteralControl("&nbsp;"));
                    }
                    if (GetDetailsViewMode() == DetailsViewMode.ReadOnly && showCloseButton)
                    {
                        this.AddButtonToCell(cell, rowIndex, "Close", "entity-close");
                        cell.Controls.Add(new LiteralControl("&nbsp;"));
                    }
                }
            }
        }

        private DetailsViewMode GetDetailsViewMode()
        {
            return (this.Control as DetailsView).CurrentMode;
        }

        private void DecorateButtons(DataControlFieldCell cell)
        {
            foreach (var ctl in cell.Controls)
            {
                if (ctl is Button)
                {
                    Button button = ctl as Button;

                    if (button.Text == CancelText)
                        button.CssClass = "entity-cancel";

                    else if (button.Text == DeleteText)
                        button.CssClass = "entity-delete";

                    else if (button.Text == InsertText)
                        button.CssClass = "entity-save";


                    else if (button.Text == EditText)
                    {
                        button.CssClass = "entity-modify";
                        if (ReadOnly)
                            button.Visible = false;
                    }

                    else if (button.Text == SelectText)
                        button.CssClass = "entity-select";

                    else if (button.Text == UpdateText)
                        button.CssClass = "entity-save";

                    else if (button.Text == NewText)
                    {
                        button.CssClass = "entity-add";
                        if (ReadOnly)
                            button.Visible = false;
                    }

                }
            }
        }

        private void AddLinkToCell(DataControlFieldCell cell, int rowIndex, string command, string style)
        {
            LinkButton anchor = new LinkButton()
            {
                CssClass = style,
                CommandName = command,
                CommandArgument = rowIndex.ToString(),
            };
            cell.Controls.Add(anchor);
        }

        private void AddButtonToCell(DataControlFieldCell cell, int rowIndex, string command, string style)
        {
            Button button = new Button()
            {
                CssClass = style,
                CommandName = command,
                CommandArgument = rowIndex.ToString(),
            };
            cell.Controls.Add(button);
        }
    }


    /// <summary>
    /// CustomizeBoundField with MaxLength , default for 100
    /// </summary>
    public class CustomizeBoundField : BoundField
    {
        public string MaxLength { get; set; }

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(cell, cellType, rowState, rowIndex);
            switch (cellType)
            {
                case DataControlCellType.DataCell:
                    if (!string.IsNullOrEmpty(MaxLength))
                    {
                        cell.Attributes.Add("maxlength", MaxLength);
                    }
                    else
                    {
                        cell.Attributes.Add("maxlength", "100");
                    }
                    break;
                    
                default:
                    break;

            }
        }
    }

    /// <summary>
    /// CustomizeBoundField with MaxLength , default for 100
    /// </summary>
    public class CustomizeTemplateField : TemplateField
    {
        public string MaxLength { get; set; }

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(cell, cellType, rowState, rowIndex);
            switch (cellType)
            {
                case DataControlCellType.DataCell:
                    if (!string.IsNullOrEmpty(MaxLength))
                    {
                        cell.Attributes.Add("maxlength", MaxLength);
                    }
                    else
                    {
                        cell.Attributes.Add("maxlength", "100");
                    }
                    break;

                default:
                    break;

            }
        }
    }

    // untested
    public class ToolTipsBoundField : BoundField
    {
        public string MaxCharacter { get; set; }

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(cell, cellType, rowState, rowIndex);
            switch (cellType)
            {
                case DataControlCellType.DataCell:
                    Label labelShow = new Label();
                    labelShow.DataBinding += new EventHandler(labelShow_DataBinding);
                    cell.Controls.Clear();
                    cell.Controls.Add(labelShow);
                    break;

                default:
                    break;

            }
        }

        private void labelShow_DataBinding(object sender, EventArgs e)
        {
            Label label = sender as Label;
            Control container = label.NamingContainer;
            object dataItem = DataBinder.GetDataItem(container);
            object dataFieldValue = null;
            if (this.DataField.Contains("."))
            {
                dataFieldValue = DataBinder.Eval(dataItem, this.DataField);
            }
            else
            {
                dataFieldValue = DataBinder.GetPropertyValue(dataItem, this.DataField);
            }
            if (dataFieldValue != null)
            {
                if (dataFieldValue.ToString().Length > Convert.ToInt32(MaxCharacter))
                {
                    label.Text = dataFieldValue.ToString().Substring(0, Convert.ToInt32(MaxCharacter));
                    label.ToolTip = dataFieldValue.ToString();
                }
                else { 
                    label.Text = dataFieldValue.ToString();
                }
            }
        }
    }


    // ToolTipsBoundColumn for DataGrid cut the string that is longer than MaxCharacter and tips
    // show all
    public class ToolTipsBoundColumn : BoundColumn
    {
        public string MaxCharacter { get; set; }

        public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
        {
            base.InitializeCell(cell, columnIndex, itemType);
            switch (itemType)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    if (!string.IsNullOrEmpty(MaxCharacter))
                        cell.DataBinding += new EventHandler(cell_DataBinding);
                    break;
                default:
                    break;
            }

        }

        private void cell_DataBinding(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(MaxCharacter))
            {
                TableCell tblCell = (TableCell)sender;
                DataGridItem dgiAge = (DataGridItem)tblCell.NamingContainer;
                PropertyDescriptor _prpDesc = TypeDescriptor.GetProperties(dgiAge.DataItem).Find(this.DataField, true);

                string textProposed = _prpDesc.GetValue(dgiAge.DataItem).ToString();
                if (textProposed.Length > Convert.ToInt32(MaxCharacter))
                {
                    tblCell.Text = string.Format("<span title=\"{0}\">{1}...</span>", textProposed, textProposed.Substring(0, Convert.ToInt32(MaxCharacter)));
                }
                else
                {
                    tblCell.Text = string.Format("<span>{0}</span>", textProposed);
                }
            }
        }
    }

}
